package com.jilian.mydemo.modul.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.BannerDto;
import com.jilian.mydemo.dialog.LoadingDialog;
import com.jilian.mydemo.modul.main.adapter.SplashViewAdapter;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.utils.SPUtil;

import java.util.List;

/**
 * 闪屏页面
 *
 * @author weishixiong
 * @Time 2018-05-23
 */
public class SplashActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private SplashViewAdapter splashViewAdapter;
    private List<BannerDto> list;
    private TextView tvCom;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_splash);
        tvCom = (TextView) findViewById(R.id.tv_com);
        SPUtil.putData(Constant.SP_VALUE.CONSTANT, Constant.SP_VALUE.ISCOMIN, true);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        String str = getIntent().getStringExtra("list");
        list = JSONObject.parseArray(str, BannerDto.class);
        if (list.size() == 1) {
            tvCom.setVisibility(View.VISIBLE);
        }
        splashViewAdapter = new SplashViewAdapter(SplashActivity.this, list);
        viewPager.setAdapter(splashViewAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == list.size() - 1) {
                    tvCom.setVisibility(View.VISIBLE);
                } else {
                    tvCom.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        tvCom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //如果没有 引导页   去登录
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MyApplication.clearSpecifyActivities(new Class[]{WelcomeActivity.class});
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }
    private LoadingDialog loadingDialog;//加载提示框


    public LoadingDialog getLoadingDialog() {
        if (null == loadingDialog) {
            loadingDialog = new LoadingDialog(this);
            //点击空白处Dialog不消失
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        return loadingDialog;
    }

    public void showLoadingDialog() {
        getLoadingDialog().showDialog();
    }

    public void hideLoadingDialog()  {
        getLoadingDialog().dismiss();
    }
}
